  <template>
  <el-table style="width: 100%" border :data="getRoleList">
    <el-table-column prop="id" label="角色编号" width="180"> </el-table-column>
    <el-table-column prop="rolename" label="角色名称" width="180">
    </el-table-column>
    <el-table-column label="状态">
      <template slot-scope="item">
        <!-- tag标签 -->
        <div>
          <el-tag v-if="item.row.status == 1" type="success">启 用</el-tag>
          <el-tag v-else type="danger">禁 用</el-tag>
        </div>
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <!-- 作用域槽口 -->
      <template slot-scope="item">
        <div>
          <el-button
            type="primary"
            round
            size="small"
            @click="edit(item.row.id)"
            >编辑</el-button
          >
          <el-button type="danger" round size="small" @click="del(item.row.id)"
            >删除</el-button
          >
        </div>
      </template>
    </el-table-column>
  </el-table>
</template>

<script>
import { mapGetters, mapActions } from "vuex";
// 引入封装好的接口模块
import { deleteRole } from "../../util/axios";
export default {
  data() {
    return {};
  },
  computed: {
    ...mapGetters({
      // store模块添加了命名空间，所以要修改名字
      getRoleList: "role/getRoleList",
    }),
  },
  methods: {
    ...mapActions({
      getRoleListAction: "role/getRoleListAction",
    }),
    // 封装编辑事件，传递id给role这个父组件
    edit(id) {
      this.$emit("edit", id);
    },
    // 封装一个删除事件
    del(id) {
      this.$confirm("你确定要删除这条数据吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 调取删除接口
          deleteRole({ id }).then((res) => {
            // console.log(res, "删除角色");
            if (res.data.code === 200) {
              this.$message.success(res.data.msg);
              // 删除之后重新调取列表
              this.getRoleListAction();
            } else {
              this.$message.error(res.data.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  mounted() {
    // 组件一加载就触发这个获取角色列表的行动
    this.getRoleListAction();
  },
};
</script>

<style lang="" scoped>
</style>
